package org.jeecg.modules.basic.warehouseGroup.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.mapper.WmsBasicWarehouseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupUser;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupWarehouse;
import org.jeecg.modules.basic.warehouseGroup.mapper.WmsBasicWarehouseGroupUserMapper;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupUserService;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupWarehouseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * @Description: 仓库组-仓管员
 * @Author: jeecg-boot
 * @Date:   2023-08-02
 * @Version: V1.0
 */
@Service
public class WmsBasicWarehouseGroupUserServiceImpl extends ServiceImpl<WmsBasicWarehouseGroupUserMapper, WmsBasicWarehouseGroupUser> implements IWmsBasicWarehouseGroupUserService {

	@Resource
	private WmsBasicWarehouseGroupUserMapper wmsBasicWarehouseGroupUserMapper;

	@Resource
	private WmsBasicWarehouseMapper basicWarehouseMapper;

	@Resource
	private IWmsBasicWarehouseGroupWarehouseService warehouseGroupWarehouseService;

	@Override
	public List<WmsBasicWarehouseGroupUser> selectByMainId(String mainId) {
		return wmsBasicWarehouseGroupUserMapper.selectByMainId(mainId);
	}

	/**
	 * 获取当前登录用户拥有的仓库id集合
	 * @return
	 */
	public List<String> getLoginUserWarehouseIdList(){
		List<String> warehouseIdList=new ArrayList<>();
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(Objects.isNull(sysUser)){
			return warehouseIdList;
		}
		if(StrUtil.contains(sysUser.getUsername(),"admin")){
			//是管理员,获取所有的仓库id
			List<WmsBasicWarehouse> wmsBasicWarehouseList = basicWarehouseMapper.selectList(new LambdaQueryWrapper<>());
			warehouseIdList=wmsBasicWarehouseList.stream().map(WmsBasicWarehouse::getId).collect(Collectors.toList());
		}else{
			//不是管理员
			warehouseIdList = wmsBasicWarehouseGroupUserMapper.getWarehouseListByUserId(sysUser.getId());
		}
		return warehouseIdList;
	}

	/**
	 * 查询当前用户管理的仓库id
	 * @return
	 */
	@Override
	public List<String> listWarehouseByLoginUser() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<WmsBasicWarehouseGroupUser> groups = this.list(new LambdaQueryWrapper<WmsBasicWarehouseGroupUser>()
				.eq(WmsBasicWarehouseGroupUser::getUserId, sysUser.getId()));
		if (CollectionUtils.isEmpty(groups)) {
			return Collections.emptyList();
		}
		Set<String> groupIds = groups.stream().map(WmsBasicWarehouseGroupUser::getWmsBasicWarehouseGroupId).collect(Collectors.toSet());
		List<WmsBasicWarehouseGroupWarehouse> list = warehouseGroupWarehouseService.list(new LambdaQueryWrapper<WmsBasicWarehouseGroupWarehouse>()
				.in(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseGroupId, groupIds));
		Set<String> set = list.stream().map(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseId).collect(Collectors.toSet());
		return new ArrayList<>(set);
	}
}
